<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="任务名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <el-form-item label="设备名称" prop="machineryId">
        <el-select v-model="queryParams.machineryId" placeholder="请选择设备名称" clearable filterable @click.native="getData">
          <el-option v-for="(item,index) in protaskList" :key="index" :label="item.machineryName" :value="item.machineryId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生产日期">
        <el-date-picker
          v-model="daterangeReportingDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始生产日期"
          end-placeholder="结束生产日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['mes:pro:process:add']">新增</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:pro:process:edit']"
        >修改</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['mes:pro:process:remove']">删除</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['mes:pro:process:export']">导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" class="draggable-table" row-key="taskId" :data="processList">
      <el-table-column label="设备名称" align="center" prop="machineryName"></el-table-column>
      <el-table-column label="任务编号" align="center" prop="taskId"></el-table-column>
      <el-table-column label="任务名称" align="center" prop="taskName"></el-table-column>
      <el-table-column label="需求日期" align="center" prop="requestDate"></el-table-column>
      <el-table-column label="开始生产日期" align="center" prop="startTime"></el-table-column>
      <el-table-column label="结束生产日期" align="center" prop="endTime"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button type="text" icon="el-icon-paperclip" @click="handeidt(scope.row)">查看</el-button> -->
          <el-button type="text" icon="el-icon-rank">拖拽排序</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 查看详情 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-row>
          <el-col :span="7">
            <el-form-item label="设备类型名称" prop="machineryName">
              <el-input v-model="form.machineryName" placeholder="请输入设备类型名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备类型编码" prop="machineryCode">
              <el-input v-model="form.machineryCode" placeholder="请输入设备类型编码" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="产品物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请输入产品物料编码" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="产品物料名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入产品物料名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="工序名称" prop="processName">
              <el-input v-model="form.processName" placeholder="请输入工序名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" placeholder="请输入客户名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排产数量" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入工序编码" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="生产时长" prop="duration">
              <el-input v-model="form.duration" placeholder="请输入生产时长" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="任务编号" prop="taskCode">
              <el-input v-model="form.taskCode" placeholder="请输入任务编号" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务名称" prop="taskName">
              <el-input v-model="form.duration" placeholder="请输入任务名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="工单名称" prop="workorderName">
              <el-input v-model="form.duration" placeholder="请输入工单名称" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType == 'view'">返回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProtaskInsert, Transfers } from "@/api/mes/pro/protaskInsertclass";
import { listMachinery } from "@/api/mes/dv/machinery";
import Sortable from "sortablejs";
export default {
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 生产日期数组
      daterangeReportingDate:[],
      // 总条数
      total: 0,
      // 生产工序表格数据
      processList: [],
      protaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName:'',
        machineryId: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.transFers();
  },
  methods: {
    /** 查询生产工序列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeReportingDate && '' != this.daterangeReportingDate) {
        this.queryParams.params["beginStartTime"] = this.daterangeReportingDate[0];
        this.queryParams.params["endStartTime"] = this.daterangeReportingDate[1];
      }
      listProtaskInsert(this.queryParams).then((response) => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    transFers() {
      const tbody = document.querySelector(
        ".draggable-table .el-table__body-wrapper tbody"
      );
      new Sortable(tbody, {
        animation: 100,
        //需要在odEnd方法中处理原始eltable数据，使原始数据与显示数据保持顺序一致
        onEnd: ({ newIndex, oldIndex }) => {
          if (newIndex != oldIndex) {
            Transfers({
              taskId: this.processList[oldIndex].taskId,
              taskIdTwo: this.processList[newIndex].taskId,
            }).then((res) => {
              this.$modal.msgSuccess("切换成功");
              this.getList();
              const targetRow = this.processList[oldIndex];
              this.processList.splice(oldIndex, 1);
              this.processList.splice(newIndex, 0, targetRow);
            });
          }
        },
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 工艺名称
    getData() {
      listMachinery().then((res) => {
        this.protaskList = res.rows;
      });
    },
    reset() {
      this.form = {};
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReportingDate = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 查看详情
    // handeidt(row) {
    //   getProtask(row.taskId).then((res) => {
    //     this.title = "任务详情";
    //     this.form = res.data;
    //     this.open = true;
    //   });
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/protask/export",
        {
          ...this.queryParams,
        },
        `process_${new Date().getTime()}.xlsx`
      );
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("PROCESS_CODE").then((response) => {
          this.form.processCode = response;
        });
      } else {
        this.form.processCode = null;
      }
    },
  },
};
</script>
